package com.allen.webservice.jaxws.config;

import com.allen.webservice.jaxws.service.StudentService;
import com.allen.webservice.jaxws.service.impl.StudentServiceImpl;
import org.apache.cxf.bus.spring.SpringBus;
import org.apache.cxf.endpoint.Server;
import org.apache.cxf.feature.LoggingFeature;
import org.apache.cxf.jaxws.JaxWsServerFactoryBean;
import org.apache.cxf.spring.boot.autoconfigure.CxfAutoConfiguration;
import org.apache.cxf.transport.servlet.CXFServlet;
import org.springframework.boot.autoconfigure.condition.ConditionalOnClass;
import org.springframework.context.annotation.Bean;
import org.springframework.context.annotation.Configuration;

import javax.annotation.Resource;
import java.util.Arrays;

@Configuration
@ConditionalOnClass(CXFServlet.class)
public class JaxwsConfig {

    @Resource
    CxfAutoConfiguration cxfAutoConfiguration;

    @Resource
    private SpringBus bus;


    @Bean("StudentServiceImplWs")
    public StudentService studentService() {
        return new StudentServiceImpl();
    }


    /**
     * 第一种服务发布方式
     * ServerFactoryBean进行服务发布
     */
    @Bean
    public Server jaxWsServer() {
        JaxWsServerFactoryBean wsEndpoint = new JaxWsServerFactoryBean();
        wsEndpoint.setBus(bus);
        wsEndpoint.setAddress("/soap/");
        wsEndpoint.setServiceClass(StudentService.class);
        wsEndpoint.setServiceBean(studentService());
        wsEndpoint.setFeatures(Arrays.asList(new LoggingFeature()));
        return wsEndpoint.create();
    }


    /**
     * 第二种服务发布方式
     * 服务端点方式或CXFServlet发布服务
     */
//    @Bean
//    public Endpoint printerpEndpoint() {
//        EndpointImpl endpoint = new EndpointImpl(bus, studentService());
//        endpoint.publish("/api");
//        endpoint.getFeatures().add(new LoggingFeature());
//        return endpoint;
//    }
//
//    @Bean
//    public ServletRegistrationBean cxfServletRegistration() {
//        return new ServletRegistrationBean(new CXFServlet(), "/soap/*");
//    }


}
